#include "sdk.h"
#include "debuggersettingscommonpanel.h"
#include <wx/fontdlg.h>
#include "debuggermanager.h"

const long DebuggerSettingsCommonPanel::ID_AUTOBUILD = wxNewId();
const long DebuggerSettingsCommonPanel::ID_AUTOSWITCH = wxNewId();
const long DebuggerSettingsCommonPanel::ID_DEBUGGERS_LOG = wxNewId();
const long DebuggerSettingsCommonPanel::ID_JUMP_ON_DOUBLE_CLICK = wxNewId();
const long DebuggerSettingsCommonPanel::ID_REQUIRE_CTRL_FOR_TOOLTIPS = wxNewId();
const long DebuggerSettingsCommonPanel::ID_VALUE_TOOLTIP_LABEL = wxNewId();
const long DebuggerSettingsCommonPanel::ID_BUTTON_CHOOSE_FONT = wxNewId();
const long DebuggerSettingsCommonPanel::ID_CHOICE_PERSPECTIVE = wxNewId();

BEGIN_EVENT_TABLE( DebuggerSettingsCommonPanel, wxPanel )
END_EVENT_TABLE()

DebuggerSettingsCommonPanel::DebuggerSettingsCommonPanel( wxWindow* parent ) {
  wxFlexGridSizer* flexSizer;
  wxBoxSizer* mainSizer;
  wxButton* chooseFont;
  wxStaticBoxSizer* valueTooltipSizer;
  wxBoxSizer* BoxSizer1;
  wxStaticText* labelPerspective;
  Create( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T( "wxID_ANY" ) );
  mainSizer = new wxBoxSizer( wxVERTICAL );
  flexSizer = new wxFlexGridSizer( 0, 1, 0, 0 );
  m_autoBuild = new wxCheckBox( this, ID_AUTOBUILD, _T( "Auto-build project if it is not up-to-date" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_AUTOBUILD" ) );
  m_autoBuild->SetValue( false );
  flexSizer->Add( m_autoBuild, 1, wxTOP | wxLEFT | wxRIGHT | wxALIGN_TOP, 5 );
  m_autoSwitch = new wxCheckBox( this, ID_AUTOSWITCH, _T( "When stopping, auto-switch to the first frame with valid source info" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_AUTOSWITCH" ) );
  m_autoSwitch->SetValue( false );
  flexSizer->Add( m_autoSwitch, 1, wxTOP | wxLEFT | wxRIGHT | wxALIGN_TOP, 5 );
  m_debuggersLog = new wxCheckBox( this, ID_DEBUGGERS_LOG, _T( "Full (Debug) log" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_DEBUGGERS_LOG" ) );
  m_debuggersLog->SetValue( false );
  flexSizer->Add( m_debuggersLog, 1, wxTOP | wxLEFT | wxRIGHT | wxALIGN_TOP, 5 );
  m_jumpOnDoubleClick = new wxCheckBox( this, ID_JUMP_ON_DOUBLE_CLICK, _T( "Jump on Double-click in Stack trace window" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_JUMP_ON_DOUBLE_CLICK" ) );
  m_jumpOnDoubleClick->SetValue( false );
  flexSizer->Add( m_jumpOnDoubleClick, 1, wxTOP | wxLEFT | wxRIGHT | wxALIGN_TOP, 5 );
  m_requireCtrlForTooltips = new wxCheckBox( this, ID_REQUIRE_CTRL_FOR_TOOLTIPS, _T( "Require Control key to show the \'Evaluate expression\' tooltips" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_REQUIRE_CTRL_FOR_TOOLTIPS" ) );
  m_requireCtrlForTooltips->SetValue( false );
  flexSizer->Add( m_requireCtrlForTooltips, 1, wxTOP | wxLEFT | wxRIGHT | wxALIGN_TOP, 5 );
  valueTooltipSizer = new wxStaticBoxSizer( wxHORIZONTAL, this, _T( "Value Tooltip Font" ) );
  m_valueTooltipLabel = new wxStaticText( this, ID_VALUE_TOOLTIP_LABEL, _T( "This is a sample text" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_VALUE_TOOLTIP_LABEL" ) );
  valueTooltipSizer->Add( m_valueTooltipLabel, 1, wxALIGN_CENTER_VERTICAL, 5 );
  chooseFont = new wxButton( this, ID_BUTTON_CHOOSE_FONT, _T( "Choose" ), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON_CHOOSE_FONT" ) );
  valueTooltipSizer->Add( chooseFont, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 5 );
  flexSizer->Add( valueTooltipSizer, 1, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 5 );
  BoxSizer1 = new wxBoxSizer( wxHORIZONTAL );
  labelPerspective = new wxStaticText( this, wxID_ANY, _T( "Perspective:" ), wxDefaultPosition, wxDefaultSize, 0, _T( "wxID_ANY" ) );
  BoxSizer1->Add( labelPerspective, 0, wxALIGN_CENTER_VERTICAL, 5 );
  m_perspective = new wxChoice( this, ID_CHOICE_PERSPECTIVE, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T( "ID_CHOICE_PERSPECTIVE" ) );
  m_perspective->Append( _T( "Only one perspective" ) );
  m_perspective->Append( _T( "One perspective per Debugger" ) );
  m_perspective->SetSelection( m_perspective->Append( _T( "One perspective per Debugger configuration" ) ) );
  BoxSizer1->Add( m_perspective, 1, wxLEFT | wxEXPAND, 5 );
  flexSizer->Add( BoxSizer1, 1, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 5 );
  mainSizer->Add( flexSizer, 1, wxALL | wxALIGN_LEFT, 0 );
  SetSizer( mainSizer );
  mainSizer->Fit( this );
  mainSizer->SetSizeHints( this );
  Connect( ID_BUTTON_CHOOSE_FONT, wxEVT_COMMAND_BUTTON_CLICKED, ( wxObjectEventFunction )&DebuggerSettingsCommonPanel::OnChooseFontClick );
  m_autoBuild->SetValue( M_Code_DebuggerCommonConfig::GetFlag( M_Code_DebuggerCommonConfig::AutoBuild ) );
  m_autoBuild->SetToolTip( _T( "Automatic project build before debug session is started." ) );
  m_autoSwitch->SetValue( M_Code_DebuggerCommonConfig::GetFlag( M_Code_DebuggerCommonConfig::AutoSwitchFrame ) );
  m_autoSwitch->SetToolTip( _T( "When stopping, auto-switch to first frame with valid source info." ) );
  m_debuggersLog->SetValue( M_Code_DebuggerCommonConfig::GetFlag( M_Code_DebuggerCommonConfig::ShowDebuggersLog ) );
  m_debuggersLog->SetToolTip( _T( "If enabled, the debugger's raw input/output will be logged in a separate log page." ) );
  m_jumpOnDoubleClick->SetValue( M_Code_DebuggerCommonConfig::GetFlag( M_Code_DebuggerCommonConfig::JumpOnDoubleClick ) );
  m_requireCtrlForTooltips->SetValue( M_Code_DebuggerCommonConfig::GetFlag( M_Code_DebuggerCommonConfig::RequireCtrlForTooltips ) );
  m_requireCtrlForTooltips->SetToolTip( _T( "If enabled, show 'Evaluate expression' tooltips only if the Control key is pressed." ) );
  m_valueTooltipFontInfo = M_Code_DebuggerCommonConfig::GetValueTooltipFont();
  UpdateValueTooltipFont();
  m_perspective->Select( M_Code_DebuggerCommonConfig::GetPerspective() );
}

DebuggerSettingsCommonPanel::~DebuggerSettingsCommonPanel() {
}

void DebuggerSettingsCommonPanel::SaveChanges() {
  M_Code_DebuggerCommonConfig::SetFlag( M_Code_DebuggerCommonConfig::AutoBuild, m_autoBuild->GetValue() );
  M_Code_DebuggerCommonConfig::SetFlag( M_Code_DebuggerCommonConfig::AutoSwitchFrame, m_autoSwitch->GetValue() );
  M_Code_DebuggerCommonConfig::SetFlag( M_Code_DebuggerCommonConfig::ShowDebuggersLog, m_debuggersLog->GetValue() );
  M_Code_DebuggerCommonConfig::SetFlag( M_Code_DebuggerCommonConfig::JumpOnDoubleClick, m_jumpOnDoubleClick->GetValue() );
  M_Code_DebuggerCommonConfig::SetFlag( M_Code_DebuggerCommonConfig::RequireCtrlForTooltips,
                                        m_requireCtrlForTooltips->GetValue() );
  M_Code_DebuggerCommonConfig::SetValueTooltipFont( m_valueTooltipFontInfo );
  M_Code_DebuggerCommonConfig::SetPerspective( m_perspective->GetSelection() );
}

void DebuggerSettingsCommonPanel::OnChooseFontClick( M_Code_unused wxCommandEvent& event ) {
  wxNativeFontInfo fontInfo;
  fontInfo.FromString( m_valueTooltipFontInfo );
  wxFontData data;
  data.SetInitialFont( wxFont( fontInfo ) );
  wxFontDialog dlg( this, data );
  PlaceWindow( &dlg );
  if( dlg.ShowModal() == wxID_OK ) {
    m_valueTooltipFontInfo = dlg.GetFontData().GetChosenFont().GetNativeFontInfo()->ToString();
    UpdateValueTooltipFont();
  }
}

void DebuggerSettingsCommonPanel::UpdateValueTooltipFont() {
  wxNativeFontInfo fontInfo;
  fontInfo.FromString( m_valueTooltipFontInfo );
  m_valueTooltipLabel->SetFont( wxFont( fontInfo ) );
}

